package com.jyw.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.system.domain.SysDeptPhoneBook;
import com.jyw.system.domain.dto.*;
import com.jyw.system.domain.vo.SysDeptPhoneBookVo;
import com.jyw.system.mapper.SysDeptPhoneBookMapper;
import com.jyw.system.service.ISysDeptPhoneBookService;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 科室电话薄Service业务层处理
 *
 * @author zhangcheng
 * @date 2024-01-10
 */
@Service
public class SysDeptPhoneBookServiceImpl extends ServiceImpl<SysDeptPhoneBookMapper, SysDeptPhoneBook> implements ISysDeptPhoneBookService {
    @Resource
    private SysDeptPhoneBookMapper sysDeptPhoneBookMapper;

    @Override
    public List<SysDeptPhoneBookVo> getBookList(SysDeptPhoneBookPageDto pageDto) {
        List<SysDeptPhoneBookVo> list = sysDeptPhoneBookMapper.getBookList(pageDto);
        return ConversionUtil.conversionList(list, SysDeptPhoneBookVo.class);
    }

    @Override
    public SysDeptPhoneBookVo getDeptByPhone(String phone) {
        if(StrUtil.isNotEmpty(phone)){
            SysDeptPhoneBookVo phoneBook=sysDeptPhoneBookMapper.getDeptByPhone(phone);
            if(phoneBook!=null){
                List<SysDeptPhoneBookVo> list=sysDeptPhoneBookMapper.getDeptListByPhone(phone);
                if(CollUtil.isNotEmpty(list)){
                    List<Long> deptIdList=list.stream().map(SysDeptPhoneBookVo::getDeptId)
                            .collect(Collectors.toList());
                    phoneBook.setDeptIdList(deptIdList);
                }
            }
            return phoneBook;
        }
        return null;
    }

    @Override
    public SysDeptPhoneBookVo getInfoById(Long id) {
        SysDeptPhoneBook sysDeptPhoneBook = this.getById(id);
        if (sysDeptPhoneBook != null) {
            SysDeptPhoneBookVo phoneBookVo = BeanUtil.toBean(sysDeptPhoneBook, SysDeptPhoneBookVo.class);
            return ConversionUtil.conversion(phoneBookVo, SysDeptPhoneBookVo.class);
        }
        return null;
    }

    @Override
    public void sysSave(SysDeptPhoneBookSaveDto saveDto) {
        if(CollUtil.isNotEmpty(saveDto.getDeptIdList())){
            String phone=saveDto.getPhone();
            // 删除之前的
            sysDeptPhoneBookMapper.delByDeptPhone(phone);
            String contacts=saveDto.getContacts();
            AtomicReference<LocalDateTime> now=new AtomicReference<>(LocalDateTime.now());
            saveDto.getDeptIdList().forEach(deptId->{
                SysDeptPhoneBook sysDeptPhoneBook=new SysDeptPhoneBook();
                sysDeptPhoneBook.setPhone(phone);
                sysDeptPhoneBook.setContacts(contacts);
                sysDeptPhoneBook.setDeptId(deptId);
                LocalDateTime newDate = now.get().plusSeconds(1);
                now.set(newDate);
                sysDeptPhoneBook.setCreateTime(newDate);
                this.save(sysDeptPhoneBook);
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sysBatchAdd(SysDeptPhoneBookBatchAddDto batchAddDto) {
        if (CollUtil.isNotEmpty(batchAddDto.getSubList())) {
            Long deptId = batchAddDto.getDeptId();
            List<SysDeptPhoneBook> phoneBooks = new ArrayList<>();
            List<SysDeptPhoneBookSubAddDto> subList = batchAddDto.getSubList();
            subList.forEach(sub -> {
                SysDeptPhoneBook phoneBook = new SysDeptPhoneBook();
                phoneBook.setDeptId(deptId);
                phoneBook.setPhone(sub.getPhone());
                phoneBook.setContacts(sub.getContacts());
                phoneBook.setCreateTime(LocalDateTime.now());
                phoneBooks.add(phoneBook);
            });
            this.saveBatch(phoneBooks);
        }
    }

    @Override
    public void sysEdit(SysDeptPhoneBookEditDto editDto) {
        SysDeptPhoneBook phoneBook = this.getById(editDto.getId());
        if (phoneBook != null) {
            BeanUtil.copyProperties(editDto, phoneBook);
            phoneBook.setUpdateTime(LocalDateTime.now());
            this.updateById(phoneBook);
        }
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return sysDeptPhoneBookMapper.deleteSysDeptPhoneBookByIds(ids);
    }
}
